//
//  HotBaseController.m
//  BobNews
//
//  Created by lanou3g on 16/5/7.
//  Copyright © 2016年 YJC. All rights reserved.
//

#import "HotBaseController.h"
#import "HotBaseModel.h"
#import "HotMulImgViewModelCell.h"
#import "HotImgViewModelCell.h"
#import "NetWorkRequestManager.h"
#import "HotBaseModelCell.h"
#import "CycleView.h"
#import "LORefresh.h"
#import "HotDetailController.h"
@interface HotBaseController ()
@property (nonatomic,strong) NSMutableArray *hotArray;
@property (nonatomic,strong) CycleView *cycleView;
@end

@implementation HotBaseController
- (void)dealloc {
    [self.tableView removeObserver:[self.tableView defaultHeader] forKeyPath:@"contentOffset"];
    [self.tableView removeObserver:[self.tableView defaultFooter] forKeyPath:@"contentOffset"];
    [self.tableView removeObserver:[self.tableView defaultFooter] forKeyPath:@"contentSize"];
    }

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"热点";
    self.hotArray = [NSMutableArray new];
    self.cycleView = [[CycleView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200) interVal:0];
    self.tableView.tableHeaderView = self.cycleView;
    
    [self.tableView registerNib:[UINib nibWithNibName:@"HotImgViewModelCell" bundle:nil] forCellReuseIdentifier:@"HotImgViewModel"];
    [self.tableView registerNib:[UINib nibWithNibName:@"HotMulImgViewModelCell" bundle:nil] forCellReuseIdentifier:@"HotMulImgViewModel"];
    [self loadData];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    __weak typeof (self) weakSelf = self;
    [self.tableView addRefreshWithRefreshViewType:LORefreshViewTypeHeaderDefault refreshingBlock:^{
        [weakSelf.hotArray removeAllObjects];
        [weakSelf.tableView reloadData];
        [weakSelf loadData];
    }];
    [self.tableView addRefreshWithRefreshViewType:LORefreshViewTypeFooterDefault refreshingBlock:^{
        //        NSInteger count = weakSelf.hotArray.count;
        [weakSelf loadData];
    }];

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)loadData {
    [NetWorkRequestManager requestUrl:[NSURL URLWithString:@"http://c.3g.163.com/recommend/getSubDocPic?size=20&passport=&devId=8wv%2B6dOeOzvcR5U8%2BYZg5w%3D%3D&lat=TRHjW5%2FfEY2uznlYAcdU5g%3D%3D&lon=raSd6u7XWdwjVFk1yCRotg%3D%3D&version=7.0&net=wifi&ts=1462330247&sign=2kW5F3QHRgkxSDoawqBEzg6b6wiAwRQYhynzy1X7Cnl48ErR02zJ6%2FKXOnxX046I&encryption=1&canal=360_news"] RequestMethod:GET Params:nil Result:^(NSData *data, NSURLResponse *response, NSError *error) {
        if (data) {
            NSMutableDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            NSArray *dataArray = jsonDict[@"推荐"];
            NSMutableArray *cycleArray = [NSMutableArray new];
            for (NSDictionary *itemDict in dataArray) {
                HotBaseModel *baseModel = [HotBaseModel modelWithDictionary:itemDict];
                [self.hotArray addObject:baseModel];
            }
            NSDictionary *zeroDict = dataArray.firstObject;
            NSString *imgstr = zeroDict[@"img"];
            [cycleArray addObject:imgstr];
                dispatch_async(dispatch_get_main_queue(), ^{
                [self.tableView reloadData];
                self.cycleView.imgNameArray = cycleArray;
                [[self.tableView defaultHeader]endRefreshing];
                [[self.tableView defaultFooter]endRefreshing];
            });
        }
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Incomplete implementation, return the number of sections
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete implementation, return the number of rows
    return self.hotArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
 
    UITableViewCell *cell = [HotBaseModelCell cellWithTableView:tableView Model:self.hotArray[indexPath.row] IndexPath:indexPath ];
          return cell;
        // Configure the cell...
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *modelName = NSStringFromClass([self.hotArray[indexPath.row] class]);
    Class CellClass = NSClassFromString([modelName stringByAppendingString:@"Cell"]);
    return [CellClass height];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    HotDetailController *hotDetailVC = [[HotDetailController alloc]init];
    hotDetailVC.model = self.hotArray[indexPath.row];
    [self.navigationController pushViewController:hotDetailVC animated:YES];
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
